package com.xquant.transaction_management_system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xquant.transaction_management_system.mapper.TransactionMapper;
import com.xquant.transaction_management_system.model.PO.*;
import com.xquant.transaction_management_system.model.VO.TransactionControlSearchVo;
import com.xquant.transaction_management_system.model.VO.Trigger;
import com.xquant.transaction_management_system.service.FinancialInstrumentsValuationService;
import com.xquant.transaction_management_system.service.Impl.FinancialInstrumentsServiceImpl;
import com.xquant.transaction_management_system.service.PersonalConfigService;
import com.xquant.transaction_management_system.service.TransactionControlService;
import com.xquant.transaction_management_system.service.TransactionService;
import com.xquant.transaction_management_system.utils.Result;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.util.Date;

@Api("交易控制层")
@RestController
@CrossOrigin
@RequestMapping("/admin")
public class TransactionController {
    @Autowired
    private TransactionService transactionService;
    @Autowired
    private FinancialInstrumentsValuationService financialInstrumentsValuationService;
    @Autowired
    private FinancialInstrumentsServiceImpl financialInstrumentsService;
    @Autowired
    private TransactionControlService transactionControlService;
    @Autowired
    private PersonalConfigService personalConfigService;

    @PostMapping("/saveTransaction")
    public Result saveTransaction(@RequestBody Transaction transaction){
        QueryWrapper<FinancialInstrumentsValuation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("debenture_code",transaction.getDebentureCode());
        queryWrapper.orderByDesc("value_date").last("limit 1");
        FinancialInstrumentsValuation one = financialInstrumentsValuationService.getOne(queryWrapper);
        BigDecimal valuation = one.getValuation();
        transaction.setTotalValuation(valuation.multiply(transaction.getAmount()));
        transaction.setSettlementAmount(transaction.getPrice().multiply(transaction.getAmount()));
        transaction.setDebentureName(one.getDebentureName());
        double value = (transaction.getPrice().doubleValue()-one.getValuation().doubleValue())/(one.getValuation().doubleValue());
        transaction.setRealDeviation(value);
        FinancialInstruments financialInstruments = financialInstrumentsService.getByCode(transaction.getDebentureCode());
        QueryWrapper<PersonalConfig> configQueryWrapper = new QueryWrapper<>();
        configQueryWrapper.eq("transaction_account",transaction.getTransactionAccount());
        configQueryWrapper.eq("debenture_code",transaction.getDebentureCode());
        PersonalConfig config = personalConfigService.getOne(configQueryWrapper);
        if (config==null){
            transactionService.save(transaction);
            return Result.ok(0);
        }
        else {
            transactionService.save(transaction);
            TransactionControl transactionControl = new TransactionControl();
            BeanUtils.copyProperties(transaction,transactionControl);
            if (config.getPriceDeviation()<value){
                transactionControl.setPriceDeviation(value);
                transactionControlService.save(transactionControl);
                return Result.ok(1);
            }
            else {
                return Result.ok(0);
            }
        }
    }
    @GetMapping("/getTranByPages/{page}/{limit}/{transactionAccount}")
    public Result getTranByPages(@PathVariable Long page,
                             @PathVariable Long limit,
                                 @PathVariable String transactionAccount){
        Page<Transaction> pageParam = new Page<>(page,limit);
        IPage<Transaction> pageModel = transactionService.getTranByPages(pageParam,transactionAccount);
        return Result.ok(pageModel);
    }
}
